package com.partner.springsecurity;

import org.springframework.security.crypto.password.*;

import com.partner.frame.support.util.*;


public class PasswdEncoder implements PasswordEncoder 
{
//   public String encodePassword(String userPassword, Object salt) throws DataAccessException
//   {
//      String PasswordEncode = "";
//
//      try
//      {
//         PasswordEncode = CommonUtil.encodeBase64(userPassword.getBytes());
//      }
//      catch(Exception eEncode)
//      {
//         PasswordEncode = "";
//         eEncode.printStackTrace();
//      }
//
//      return PasswordEncode;
//   }
//
//   @Override
//   public boolean isPasswordValid(String userPasswd, String inputPasswd, Object salt) throws DataAccessException
//   {
//      boolean bResult = false;
//      String strInputPasswd = "";
//
//      if(inputPasswd == null || inputPasswd.trim().length() == 0)
//         bResult = false;
//      else
//      {
//         strInputPasswd = encodePassword(inputPasswd, salt);
//         System.out.println("InputPasswd[" + strInputPasswd + "] - userPasswd[" + userPasswd + "]");
//         
//         bResult = userPasswd.equals(strInputPasswd);
//      }
//
//      return bResult;
//   }


   public String encode(CharSequence passwd)
   {
      String passwdEncode = "";

      try
      {
         passwdEncode = CommonUtil.encodeBase64(passwd.toString().getBytes());
      }
      catch(Exception eEncode)
      {
         passwdEncode = "";
         eEncode.printStackTrace();
      }

      return passwdEncode;
   }

   public boolean matches(CharSequence inputPasswd, String userPasswd)
   {
      byte[] btData = null;
      boolean bStatus = false;
      String cryptPasswd = "";
      
      //verifyPasswd = Md5Crypt.md5Crypt(inputPasswd.toString().getBytes());
      btData = CommonUtil.computeMD5(inputPasswd.toString().getBytes());
      btData = CommonUtil.computeMD5(btData);
      cryptPasswd = CommonUtil.convertBytesToString(btData);
      
      //verifyPasswd = encode(inputPasswd.toString());
      //md5Passwd = CommonUtil.computeMD5(btSource)
      System.out.println("PasswdEncoder::matches - inputPasswd[" +
         inputPasswd.toString() + "] cryptPasswd[" + cryptPasswd + "] userPasswd[" + userPasswd + "]" );
      
      if(cryptPasswd.equals(userPasswd))
         bStatus = true;
      
      return bStatus;
   }
public static void main(String[] args) {
	byte[] btData = CommonUtil.computeMD5("123456".toString().getBytes());
	btData = CommonUtil.computeMD5(btData);
	String cryptPasswd = CommonUtil.convertBytesToString(btData);
	System.out.println(cryptPasswd);
}

}
